﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Kaiser.Models;
using Kaiser.Modules.Hiner.Models.Consignee.SearchModels;
using Kaiser.Modules.Hiner.Services;
using Kaiser.Modules.Hiner.Models.Consignee;
using Kaiser.Models.JsonModels;
using System.Web.Script.Serialization;
using Kaiser.Extensions;
using Kaiser.Infrastructure.Controllers;

namespace Kaiser.Modules.Hiner.Controllers
{


    public class BaseDataManageController : KaiserBaseController
    {
        #region 变量和属性
        private readonly IConsigneeService _consigneeService;

        public BaseDataManageController(IConsigneeService consigneeService)
        {
            this._consigneeService = consigneeService;
        }

        #endregion

        public ActionResult ConsigneeList()
        {
            return View();
        }
        public ActionResult ProductDAList()
        {
            return View();
        }

        /// <summary>
        /// 收货人列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="pageinfo"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult ConsigneeList(ConsigneeSearch search, PagingInfo pageinfo, FormCollection form)
        {
            search.ConsigneeCode = form["ConsigneeCode"];
            search.ConsigneeName = form["ConsigneeName"];
            var result = _consigneeService.SearchConsignee(search, pageinfo);
     
            return Content(result.ToJsonString());
        }

        /// <summary>
        /// 新增订单
        /// </summary>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        public ActionResult AddConsignee(ConsigneeBase model)
        {
            return EditConsignee("", model);
        }

        /// <summary>
        /// 编辑订单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="model"></param>
        /// <param name="form"></param>
        /// <returns></returns>
        public ActionResult EditConsignee(string id, ConsigneeBase model)
        {
            JsonReturnMessages result = new JsonReturnMessages();

            try
            {
                if (id.IsNullOrWhiteSpace())
                {
                    model.ConsigneeId = Guid.NewGuid().GenerateGuid().ToString();
                    model.CreateUserFullName = User.Identity.Name;
                    model.CreateUserName = CurrentUser.Name;
                    model.CreateDateTime = DateTime.Now;
                    _consigneeService.Insert(model);
                }
                else
                {
                    ConsigneeBase oldSort = _consigneeService.Get(id);
                    model.CreateDateTime = oldSort.CreateDateTime;
                    model.CreateUserFullName = oldSort.CreateUserFullName;
                    model.CreateUserName = oldSort.CreateUserName;
                    model.LastModifyDateTime = DateTime.Now;
                    model.LastModifyUserFullName = User.Identity.Name;
                    model.LastModifyUserName = CurrentUser.Name;
                    _consigneeService.Update(model);
                }
                result.success = true;
                result.msg = "保存成功！";
            }
            catch (Exception ex)
            {
                result.success = false;
                result.msg = ex.Message;
            }
            return Json(result, "text/html");
        }

        /// <summary>
        /// 批量删除订单
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public ActionResult DelConsignee(string ids)
        {
            JsonReturnMessages result = new JsonReturnMessages(); ;
            if (!ids.IsNullOrWhiteSpace())
            {
                try
                {
                    for (int i = 0; i < ids.Split(',').Length; i++)
                    {
                        _consigneeService.Delete(ids.Split(',')[i]);
                    }
                  
                    result.success = true;
                    result.msg = "删除成功";
                }
                catch (Exception)
                {
                    result.success = false;
                    result.msg = "删除失败";

                }
            }
            else
            {
                result.msg = "传值失败，请告诉开发者解决";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return Content(json.Serialize(result), "text/html;charset=UTF-8");
        }
    }
}
